<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello Sea.js</title>
</head>
<body>
<div id="container">
  <input type="file" id="uploadFile">
  <img src="">
</div>
<script src="/assets/jquery.js"></script>
<script src="js/modules/jm.js"></script>
<script src="js/modules/je.js"></script>
<script src="js/modules/test.js"></script>
<script>
$(function(){
	$('body').on('change', '#uploadFile', function(e) {
		var reader = new FileReader();
        var uploadBase64;
        var conf = {}, file = this.files[0];
        var ic = new ImageCompresser();
        var newfile;
        
        $.extend({
            os:{
                ios : false,
                android: false,
                version: false
            }
        });
    	
    	var ua = navigator.userAgent;
        var browser = {},
            weixin = ua.match(/MicroMessenger\/([^\s]+)/),
            webkit = ua.match(/WebKit\/([\d.]+)/),
            android = ua.match(/(Android)\s+([\d.]+)/),
            ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
            ipod = ua.match(/(iPod).*OS\s([\d_]+)/),
            iphone = !ipod && !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
            webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/),
            touchpad = webos && ua.match(/TouchPad/),
            kindle = ua.match(/Kindle\/([\d.]+)/),
            silk = ua.match(/Silk\/([\d._]+)/),
            blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/),
            mqqbrowser = ua.match(/MQQBrowser\/([\d.]+)/),
            chrome = ua.match(/CriOS\/([\d.]+)/),
            opera = ua.match(/Opera\/([\d.]+)/),
            safari = ua.match(/Safari\/([\d.]+)/);
        if (weixin) {
           $.os.wx = true;
           $.os.wxVersion = weixin[1];
        }
        // if (browser.webkit = !! webkit) browser.version = webkit[1]
        if (android) {
            $.os.android = true;
            $.os.version = android[2];
        }
        if (iphone) {
            $.os.ios = $.os.iphone = true;
            $.os.version = iphone[2].replace(/_/g, '.');
        }
        if (ipad) {
            $.os.ios = $.os.ipad = true;
            $.os.version = ipad[2].replace(/_/g, '.');
        }
        if (ipod) {
            $.os.ios = $.os.ipod = true;
            $.os.version = ipod[2].replace(/_/g, '.');
        }
        

        // wifi 下图片高质量
        if (window.NETTYPE == window.NETTYPE_WIFI) {
            conf = {
                maxW: 300, //目标宽
                maxH: 128, //目标高
                quality: 0.8, //目标jpg图片输出质量
            };
        }

        reader.onload = function(e) {
            var result = this.result;

            // 如果是jpg格式图片，读取图片拍摄方向,自动旋转
            if (file.type == 'image/jpeg'){
                try {
                    var jpg = new JpegMeta.JpegFile(result, file.name);
                } catch (e) {
                    alert('图片不是正确的图片数据')
                    $('#li' + id).remove();
                    return false;
                }
                if (jpg.tiff && jpg.tiff.Orientation) {
                    //设置旋转
                    conf = $.extend(conf, {
                        orien: jpg.tiff.Orientation.value
                    });
                }
            }

            // 压缩
            if (ic.support()) {
                var img = new Image();
                img.onload = function() {
                    try {
                        uploadBase64 = ic.getImageBase64(this, conf);
                    } catch (e) {
                    	alert(e);
                        alert('压缩图片失败')
                        $('#li' + id).remove();
                        return false;
                    }
                    if (uploadBase64.indexOf('data:image') < 0) {
                        alert('上传照片格式不支持')
                        $('#li' + id).remove();
                        return false;
                    }

                    newfile = uploadBase64;
                    $('body').find('img').attr('src', uploadBase64);
                }
                img.onerror = function() {
                    alert('解析图片数据失败')
                    $('#li' + id).remove();
                    return false;
                }
                img.src = ic.getFileObjectURL(file);
            } else {
                uploadBase64 = result;
                if (uploadBase64.indexOf('data:image') < 0) {
                    alert('上传照片格式不支持')
                    $('#li' + id).remove();
                    return false;
                }

                module.exports.uploadInfo[id].file = uploadBase64;
                $('#li' + id).find('img').attr('src', uploadBase64);
                module.exports.uploadQueue.push(id);
            }
        }

        reader.readAsBinaryString(file);
	});
});
</script>

</body>
</html>